package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.BaseMenu;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.UserMenu;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.IBaseMenuService;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IUserMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户菜单权限管理控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-08-06
 */
@RestController
@RequestMapping("/sys/usermenu")
@Api(description = "用户菜单权限管理")
public class UserMenuController extends BaseController<IUserMenuService, UserMenu> {

    @Autowired
    private IUserMenuService userMenuService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IBaseMenuService iBaseMenuService;


    @ApiOperation("获取用户权限分页")
    @RequestMapping(value = "/getAllPage", method = RequestMethod.POST)
    public ReturnObject<List<UserMenu>> getAllNewPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                     @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                      @ApiParam("用户名称") @RequestParam String name) {

        EntityWrapper<UserMenu> ew = new EntityWrapper<UserMenu>();
        ew.eq("status", 0);
        if(StringUtils.isNotBlank(name)){
            ew.like("user_name",name);
        }
        ew.orderBy("crt_time", false);
        Page<UserMenu> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        if(obj.getRecords()!=null&&obj.getRecords().size()>0){
            for (int i = 0; i < obj.getRecords().size(); i++) {
                List<Long> ids=new ArrayList<>();
                if(obj.getRecords().get(i).getMenuIds()!=null){
                    String [] idsArr=obj.getRecords().get(i).getMenuIds().split(",");
                    for (int j = 0; j <idsArr.length ; j++) {
                        ids.add(Long.valueOf(idsArr[j]));
                    }
                    List<String> menuNames=new ArrayList<>();
                    List<BaseMenu> baseMenus=iBaseMenuService.selectList(new EntityWrapper<BaseMenu>().in("id",ids));
                    if(baseMenus!=null&&baseMenus.size()>0){
                        for (int j = 0;  j<baseMenus.size() ; j++) {
                            menuNames.add(baseMenus.get(j).getName());
                        }
                    }
                    obj.getRecords().get(i).setMenuNames(menuNames);
                }

            }
        }

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取用户权限详情")
    @IgnoreAuth
    @RequestMapping(value = "/findInfoByUserId", method = RequestMethod.POST)
    public ReturnObject<UserMenu> findInfoByUserId(@ApiParam("Token") @RequestParam String Token) {

        SysUser sysUser=iSysUserService.selectOne(new EntityWrapper<SysUser>().eq(
                "token",Token));

        return new ReturnObject<>(0, "获取成功",
                baseService.selectOne(new EntityWrapper<UserMenu>().eq(
                        "user_id", sysUser.getId())));
    }


    @ApiOperation("获取用户权限详情")
    @IgnoreAuth
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<UserMenu> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "获取成功", baseService.selectById(id));
    }

    @ApiOperation("新增")
    @RequestMapping(value = "/saveUserMenu", method = RequestMethod.POST)
    public ReturnObject<UserMenu> saveIntroduce(@ApiParam("userId") @RequestParam Long userId,
                                                 @ApiParam("menuIds") @RequestParam  String menuIds) {

            UserMenu info = userMenuService.selectOne(new EntityWrapper<UserMenu>().eq("user_id",userId));
            if(info!=null){
                info.setMenuIds(menuIds);
                baseService.updateById(info);

            }else{
                SysUser sysUser=iSysUserService.selectById(userId);
                info=new UserMenu();
                info.setUserId(userId);
                info.setMenuIds(menuIds);
                info.setUseName(sysUser.getUsername());
                baseService.insert(info);
            }

        return new ReturnObject<>(0, "新增成功");
    }


}

